package com.sany.taskmanager.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.WindowManager;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;

import com.sany.taskmanager.R;
import com.sany.taskmanager.databinding.ActivitySplashBinding;
import com.sany.taskmanager.global.Key;
import com.sany.taskmanager.manager.DataManager;
import com.sany.taskmanager.util.PreferenceUtil;

/**
 * 启动页面
 */
public class SplashActivity extends AppCompatActivity {

    private final Handler handler = new Handler();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        int flags = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_FULLSCREEN
                | View.SYSTEM_UI_FLAG_IMMERSIVE;
        getWindow().getDecorView().setSystemUiVisibility(flags);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getWindow().setNavigationBarColor(ContextCompat.getColor(this, R.color.color_111929));
        ActivitySplashBinding activityBaseBinding = ActivitySplashBinding.inflate(getLayoutInflater());
        setContentView(activityBaseBinding.getRoot());

        handler.postDelayed(() -> {
            boolean isLogin = PreferenceUtil.getPreferences().getBoolean(Key.SharedKey.IS_LOGIN);
            if (isLogin) {
                String factoryCode = PreferenceUtil.getPreferences().getString(Key.SharedKey.FACTORY_CODE);
                DataManager.getInstance().setFactoryCode(factoryCode);
                // 已登录，跳转到主页面
                startActivity(new Intent(this, MainActivity.class));
            } else {
                // 未登录，跳转到登录页面
                startActivity(new Intent(this, LoginActivity.class));
            }
            finish();
        }, 1000);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        handler.removeCallbacks(null);
    }
}
